# Copyright 2025 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@pigweed//pw_build:compatibility.bzl", "incompatible_with_mcu")
load("@rules_rust//rust:defs.bzl", "rust_library")

rust_library(
    name = "console",
    srcs = ["console.rs"],
    edition = "2024",
    visibility = ["//visibility:public"],
    deps = [
        "//pw_status/rust:pw_status",
    ],
)

rust_library(
    name = "console_backend_stdio",
    srcs = ["console_backend_stdio.rs"],
    crate_name = "console_backend",
    edition = "2024",
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        "//pw_status/rust:pw_status",
    ],
)

rust_library(
    name = "console_backend_semihosting",
    srcs = ["console_backend_semihosting.rs"],
    crate_features = select({
        "@platforms//cpu:armv8-m": ["arch_arm_cortex_m"],
        "@platforms//cpu:riscv32": ["arch_riscv"],
        "//conditions:default": [],
    }),
    crate_name = "console_backend",
    edition = "2024",
    target_compatible_with = select({
        "@platforms//cpu:armv8-m": [],
        "@platforms//cpu:riscv32": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    visibility = ["//visibility:public"],
    deps = [
        "//pw_status/rust:pw_status",
    ] + select({
        "@platforms//cpu:armv8-m": ["@rust_crates//:cortex-m-semihosting"],
        "@platforms//cpu:riscv32": ["@rust_crates//:riscv-semihosting"],
        "//conditions:default": [],
    }),
)

rust_library(
    name = "colors",
    srcs = ["colors.rs"],
    crate_features = ["color"],
    edition = "2024",
    visibility = ["//visibility:public"],
    deps = [
        "//pw_bytes/rust:pw_bytes",
        "//pw_log/rust:pw_log_backend_api",
    ],
)

label_flag(
    name = "console_backend",
    build_setting_default = ":console_backend_stdio",
    tags = ["kernel"],
    visibility = ["//visibility:public"],
)

rust_library(
    name = "pw_log_backend_basic",
    srcs = [
        "pw_log_backend_basic.rs",
    ],
    crate_name = "pw_log_backend",
    edition = "2024",
    proc_macro_deps = ["//pw_kernel/lib/pw_log_helper:pw_log_backend_basic_macro"],
    visibility = ["//visibility:public"],
    deps = [
        ":colors",
        ":console",
        "//pw_log/rust:pw_log_backend_api",
    ],
)

rust_library(
    name = "pw_log_backend_tokenized",
    srcs = [
        "pw_log_backend_tokenized.rs",
    ],
    crate_name = "pw_log_backend",
    edition = "2024",
    tags = ["kernel"],
    visibility = ["//visibility:public"],
    deps = [
        ":colors",
        ":console",
        "//pw_base64/rust:pw_base64",
        "//pw_kernel/lib/pw_log_helper:tokenized_writer",
        "//pw_log/rust:pw_log_backend_api",
        "//pw_status/rust:pw_status",
        "//pw_stream/rust:pw_stream",
        "//pw_tokenizer/rust:pw_tokenizer",
    ],
)
